package com.chuanke.ckfamily.service.common.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chuanke.ckfamily.dao.base.CkUtil;
import com.chuanke.ckfamily.dao.common.TipsDao;
import com.chuanke.ckfamily.dao.common.model.Tips;
import com.chuanke.ckfamily.dao.social.model.RedpackateRule;
import com.chuanke.ckfamily.dao.user.model.User;
import com.chuanke.ckfamily.service.common.CacheService;
import com.chuanke.ckfamily.service.common.TipsService;
import com.chuanke.ckfamily.service.constant.TipsType;
import com.chuanke.ckfamily.service.exception.CFException;
import com.chuanke.ckfamily.service.social.RedpackateRuleService;
import com.clusterswisdom.kid.server.libary.util.HttpUtil;
import com.weijie.core.BaseBiz;

import freemarker.cache.StringTemplateLoader;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;

@Service
@Transactional
public class TipsServiceImpl extends BaseBiz<Tips, TipsDao> implements TipsService {
	public static final String FAMILY_RULE_HELP = "/asks/new_index?type=1&page=11";

	public static final String LEADER_INSTRUCTIONS = "/asks/new_index?type=2&page=19";
	@Autowired
	private CacheService cacheService;

	@Autowired
	private RedpackateRuleService redpackateRuleService;

	@Override
	public String getTemplate(Integer code, Map<String, Object> map) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
		cfg.setDefaultEncoding("UTF-8");
		String s = (String) cacheService.get(CacheService.KEY_PREFIX.freemarker_template_.name() + code);
		if (s == null || "".equals(s)) {
			s = baseDao.getMessageBycode(code);
			cacheService.set(CacheService.KEY_PREFIX.freemarker_template_.name() + code, s);
		}
		StringTemplateLoader stl = new StringTemplateLoader();
		stl.putTemplate("stl", s);
		cfg.setTemplateLoader(stl);
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		Template temp = cfg.getTemplate("stl", "utf-8");
		StringWriter out = new StringWriter();
		temp.process(map, out);
		 String string = out.toString();
		 return string;
	}

	@Override
	public String getFamilyIncomeTips() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> root = new HashMap<String, Object>();

		List<Map<String, Object>> activeRule = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> inviteRule = new ArrayList<Map<String, Object>>();

		Map<String, Object> activeRuleMap = new HashMap<String, Object>();
		Map<String, Object> inviteRuleMap = new HashMap<String, Object>();
		activeRuleMap.put("activeUserNum", 1);
		activeRuleMap.put("activeIncom", 0.1);

		inviteRuleMap.put("inviteUserNum", 1);
		inviteRuleMap.put("inviteIncom", 0.5);

		activeRule.add(activeRuleMap);
		inviteRule.add(inviteRuleMap);

		root.put("activeRule", activeRule);
		root.put("inviteRule", inviteRule);
		return getTemplate(TipsType.FAMILY_INCOM.getValue(), root);
	}

	@Override
	public String getFamilyRedpacketTips() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> root = new HashMap<String, Object>();
		Map<String, Object> leaderIncome = new HashMap<String, Object>();
		leaderIncome.put("leaderIncome", "65%");
		root.put("leaderIncome", leaderIncome);
		return getTemplate(TipsType.FAMILY_REDPACKET.getValue(), root);
	}

	@Override
	public String getLeaderIncomeTips() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> root = new HashMap<String, Object>();
		Map<String, Object> leaderIncome = new HashMap<String, Object>();
		leaderIncome.put("leaderIncome", "30%");
		root.put("leaderIncome", leaderIncome);
		return getTemplate(TipsType.LEADER_SHARING.getValue(), root);
	}

	@Override
	public String getLeaderSuperIncomeTips() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> root = new HashMap<String, Object>();
		Map<String, Object> leaderSuperIncome = new HashMap<String, Object>();
		leaderSuperIncome.put("leaderSuperIncome", "5%");
		root.put("leaderSuperIncome", leaderSuperIncome);
		return getTemplate(TipsType.LEADER_SUPER_SHARING.getValue(), root);
	}

	@Override
	public String getRedpackageRuleTips() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> root = new HashMap<String, Object>();
		List<RedpackateRule> redpackateRule = redpackateRuleService.getAll();
		root.put("redpackateRule", redpackateRule);
		return getTemplate(TipsType.FAMILY_REDPACKET_RULE.getValue(), root);
	}

	@Override
	public String getSendRedpackage(Long money, Long packateMoney, Long packateNum) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("money", CkUtil.formatMoney(money));
		param.put("packatemoney", packateMoney);
		param.put("packatenum", packateNum);
		return getTemplate(TipsType.SEND_FAMILY_REDPACKET.getValue(), param);
	}
	
	@Override
	public String getSendRedpackage(Long money) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("money", CkUtil.formatMoney(money));
		return getTemplate(TipsType.SEND_REDPACKET.getValue(), param);
	}

	@Override
	public String getSendRedpackage(Long money, Integer userNum) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("money", CkUtil.formatMoney(money));
		param.put("userNum", userNum);
		return getTemplate(TipsType.SEND_LEADER_REDPACKET.getValue(), param);
	}

	@Override
	public String getCreateFamily() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> param = new HashMap<String, Object>();
		return getTemplate(8, param);
	}
	
	@Override
	public String getLeader_know() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		Map<String, Object> param = new HashMap<String, Object>();
		return getTemplate(9, param);
	}

	@Override
	public Map<String, String> getHelpUrl() throws IOException {
		String s = HttpUtil.getInstance().sendHttpRequest("http://pic.daword.cn/cknote1.json.txt", HttpUtil.METHOD_GET);
		JSONObject resultObj = JSON.parseObject(s);
		String host = "http://" + resultObj.getString("apiHost");
		Map<String, String> map = new HashMap<String, String>();
		map.put("family_rule_help", host + FAMILY_RULE_HELP);
		map.put("leader_instructions", host + LEADER_INSTRUCTIONS);
		return map;
	}

	@Override
	public String getShield(boolean isOwner) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			if(isOwner){
				return getTemplate(TipsType.SHIELD_FAMILY_OWNER.getValue(), param);
			}else{
				return getTemplate(TipsType.SHIELD_FAMILY_MEMBER.getValue(), param);
			}
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
		return null;
	}

}